package com.tianqu.tms.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.UserClassDao;
import com.tianqu.tms.dto.UserClassDto;

/**
 * 用户组
 * 
 * @author tianshifu
 *
 */
public class UserClass extends TmsEntity {

	/**
	 * Serializable id
	 */
	private static final long serialVersionUID = 8941684905894069063L;
	
	private int userClass;//用户组ID
	
	private String userClassName;//用户组名


	/******************* 定义获取code信息类型 *************************/

	public final static int SYS_ADMINISTRATOR = 90;//系统管理员
	public final static int SYS_MEMBER = 11;//系统会员
	public final static int SYS_OPERATOR = 41;//系统操作员


	/**
	 * 实例化userClass表
	 * 
	 * @return
	 */
	private static UserClass instance;

	List<UserClass> codes = null;

	/**
	 * 加载userClass
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public static UserClass getInstance() throws TmsDaoException {
		if (null == instance) {
			instance = new UserClass();
			instance.init();
		}
		return instance;
	}

	/**
	 * 实例化userClass
	 * 
	 * @throws TmsDaoException
	 */
	public void init() throws TmsDaoException {
		codes = new ArrayList<UserClass>();
		setCodes();
	}

	/**
	 * 获取userClass信息存在静态codes中
	 * @throws TmsDaoException
	 */
	public void setCodes() throws TmsDaoException {
		
		UserClassDao dao = (UserClassDao) TmsDao.getInstance(TmsDao.TMS_USER_CLASS);
		List<UserClassDto> list = dao.instanceCodes();
		for (int i = 0; i < list.size(); i++) {
			UserClassDto dto = (UserClassDto) list.get(i);
			UserClass entity = new UserClass();
			entity.setUserClass(dto.getUserClass());
			entity.setUserClassName(dto.getUserClassName());
			codes.add(entity);
		}
	}

	/**
	 * 获取下拉列表
	 * 
	 * @param itemId
	 * @return
	 */
	public  List getUserClassList() {
		Map map = null;// 
		List list = new ArrayList();
		for (int i = 0; i < codes.size(); i++) {
			UserClass dto = (UserClass) codes.get(i);
				map = new HashMap();
				map.put("userClass", dto.getUserClass());
				map.put("userClassName", dto.getUserClassName());
				list.add(map);
		}
		if (list.size()==0) {
			return null;
		}		

		return list;
	}
	/**
	 * 获取下拉列表
	 * 
	 * @param itemId
	 * @return
	 */
	public  List getCreatUserList() {
		Map map = null;// 
		List list = new ArrayList();
		for (int i = 0; i < codes.size(); i++) {
				UserClass dto = (UserClass) codes.get(i);
				if(dto.getUserClass()!=SYS_MEMBER){
					map = new HashMap();
					map.put("userClass", dto.getUserClass());
					map.put("userClassName", dto.getUserClassName());
					list.add(map);
				}
		}
		if (list.size()==0) {
			return null;
		}		

		return list;
	}
	/**
	 * 根据userClass获取userClassName
	 * @return
	 */
	public String getUserClassNameByUserClass(int userClass){
		for(int i=0;i<codes.size();i++){
			UserClass entity = codes.get(i);
			if(entity.getUserClass() == userClass)
			{
				return entity.getUserClassName();
			}
		}
		
		return "";
	}
	/**
	 * 根据userClassName获取userClass
	 * @return
	 */
	public int getUserClassByUserClassName(String userClassName){
		for(int i=0;i<codes.size();i++){
			UserClass entity = codes.get(i);
			if(entity.getUserClassName().equals(userClassName))
			{
				return entity.getUserClass();
			}
		}
		return 0;
	}
	public int getUserClass() {
		return userClass;
	}

	public void setUserClass(int userClass) {
		this.userClass = userClass;
	}

	public String getUserClassName() {
		return userClassName;
	}

	public void setUserClassName(String userClassName) {
		this.userClassName = userClassName;
	}

	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub

	}

}
